/**
 * The Community Project
 * Copyright Team1 Productions
 * 
 * Joshua Amick
 * Stephen Cunningham
 * Thema Davis
 * Tomika Dawson
 * Lucie DeCristofaro
 * 
 * All Rights Reserved.
 */

package com.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.domain.User;
import com.forms.SearchProfileForm;

public class SearchProfileAction extends CommunityBaseAction{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.execute(mapping, form, request, response);

		// Cast the actionForm, if there is one.
		ActionForward forward = mapping.findForward("success");
		SearchProfileForm f = (SearchProfileForm) form;
		
		//Check user
		User usr = (User)request.getSession().getAttribute("AUTH_USER");
		
		//Get search results
		List<User> results;
		if(f.getUsername()==null&&f.getFirstName()==null&&f.getLastName()==null&&f.getMajor().equals("Major")){
			results = this.biz.getPrivateMgr().getAllUsers();
		}
		else{
			results = this.biz.getPrivateMgr().searchProfiles(f.getUsername(),f.getFirstName(),f.getLastName(),f.getMajor());
		}
		
		if(usr!=null){
			if(results!=null&&results.size()>0){
				for(int i = 0;i<results.size();i++){
					if(results.get(i).getKeyId().equals(usr.getKeyId())){
						results.remove(i);
					}
				}
			}
		}
		
		request.setAttribute("SEARCH_RESULTS", results);

		return forward;
	}
}
